package com.shop.controller.admin;

import com.shop.dto.DiscountDTO;
import com.shop.dto.DiscountPageQueryDTO;
import com.shop.entity.Discount;
import com.shop.result.PageResult;
import com.shop.result.Result;
import com.shop.service.DiscountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/discount")
@Api(tags = "折扣相关接口")
@Slf4j
public class DiscountController {

    @Autowired
    private DiscountService discountService;

    @PostMapping
    @ApiOperation("新增折扣商品")
    public Result save(@RequestBody DiscountDTO discountDTO) {
        log.info("新增折扣商品：{}", discountDTO);
        // 使用注入的实例调用方法
        discountService.save(discountDTO);
        return Result.success();
    }


    @GetMapping("/page")
    @ApiOperation("折扣商品分页查询")
    public Result<PageResult> page(DiscountPageQueryDTO discountPageQueryDTO) {
        log.info("折扣商品分页查询:{}", discountPageQueryDTO);
        PageResult pageResult = discountService.pageQuery(discountPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 商品批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("折扣商品批量删除")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("折扣商品批量删除：{}", ids);
        discountService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据id查询商品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询折扣商品")
    public Result<Discount> getById(@PathVariable Long id) {
        log.info("根据id查询折扣商品：{}", id);
        Discount discount = discountService.getById(id);
        return Result.success(discount);
    }

    /**
     * 修改商品
     *
     * @param discountDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改折扣商品")
    public Result update(@RequestBody DiscountDTO discountDTO) {
        log.info("修改折扣商品：{}", discountDTO);
        discountService.update(discountDTO);
        return Result.success();
    }

    /**
     * 商品起售停售
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("折扣商品起售停售")
    public Result<String> startOrStop(@PathVariable Integer status, @RequestParam Long id) {
        discountService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 根据分类id查询商品
     *
     * @param discountType
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据折扣类型查询折扣商品")
    public Result<List<Discount>> list(@RequestParam Long discountType) {
        List<Discount> list = discountService.list(discountType);
        return Result.success(list);
    }

}